<template>
	<view class="cate">
		<!-- 搜索框 -->
		<my-search @click="toSearch" bgColor="#eb4450" radius="20"></my-search>
		<!-- 分类 -->
		<view class="mycate">
			<scroll-view class="left" scroll-y="true" :style="{height: `${wh}px`}">
				<view class="left-item" :class="{active: activeIndex === index}" @click="changeIndex(index)"
					v-for="(item, index) in cateList" :key="index">{{item.cat_name}}</view>
			</scroll-view>
			<scroll-view :scroll-top="scrollTop" class="right" scroll-y="true" :style="{height: `${wh}px`}">
				<view class="two" v-for="(item, index) in twiceList" :key="index">
					<view class="title">
						/<text>{{item.cat_name}}</text>/
					</view>
					<view class="three">
						<view @click="toList(subitem.cat_name)" class="three-item"
							v-for="(subitem, subindex) in item.children" :key="subindex">
							<image :src="subitem.cat_icon" mode=""></image>
							<view>{{ subitem.cat_name }}</view>
						</view>
					</view>
				</view>

			</scroll-view>
		</view>

	</view>
</template>

<script>
	import mymixins from '../../mixins/mymixins.js'
	export default {
		// 使用混入
		mixins: [mymixins],
		data() {
			return {
				wh: '',
				// 设置当前选中的一级分类
				activeIndex: 0,
				// 分类数据源
				cateList: [],
				// 二级分类
				twiceList: [],
				// 重置滚动距离
				scrollTop: 0,

			};
		},
		onLoad() {
			// 得到容器可使用高度
			uni.getSystemInfo({
					success: res => {
						this.wh = res.windowHeight - 60
					}
				}),
				this.getCateList()
		},

		methods: {
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				// console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				// 保存数据
				this.cateList = res.message
				// 得到对应的二级分类
				this.twiceList = this.cateList[this.activeIndex].children
				// console.log(this.twiceList);
				// 得到对应的三级分类
				// 重置滚动剧场
				this.scrollTop = this.scrollTop === 0 ? 1 : 0

			},
			changeIndex(index) {
				this.activeIndex = index
				// 修改对应的二级分类
				this.twiceList = this.cateList[this.activeIndex].children

			},
			// 跳转到商品列表页面
			toList(name) {
				uni.navigateTo({
					url: `/pages/list/list?key=${name}`
				})
			},
			toSearch() {
				uni.navigateTo({
					url: '/pages/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.mycate {
		display: flex;

		.left {
			width: 200rpx;

			.left-item {
				position: relative;
				background-color: #f4f4f4;
				height: 100rpx;
				border-bottom: 1px solid #eee;
				color: #333;
				text-align: center;
				line-height: 100rpx;

				&.active {
					background-color: #fff;
					color: #eb4450;

					&::before {
						content: '';
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
						width: 8rpx;
						height: 60rpx;
						background-color: #eb4450;


					}
				}

			}
		}

		.right {
			flex: 1;

			.title {
				height: 110rpx;
				color: #e0e0e0;
				text-align: center;
				line-height: 110rpx;

				text {
					color: #333;
					margin: 0 30rpx;
				}

			}

			.three {
				display: flex;
				flex-wrap: wrap;

				.three-item {
					width: 33.33%;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;

					image {
						width: 120rpx;
						height: 120rpx;
					}
				}
			}
		}
	}
</style>
